<template lang="pug">
  el-drawer.commonDrawer(
    :title="`${type === 'add' ? '新增' : type === 'update' ? '修改' : '查看'}直播`"
    :visible.sync="visibleDialog"
    v-loading="loading"
    custom-class="commonDrawer"
    direction="rtl"
    size="840px"
    append-to-body
    destroy-on-close
    modal-append-to-body
  )
    .drawer-content
      form-box(
        ref="basicForm"
        :fields="basicFormFields"
        :showBtns="false"
        labelWidth="126px"
        :disabled="disabled"
        :layoutCol="{ sm: 24, md: 24, lg: 24 }"
      )
    .drawer-footer.tr(v-if="!disabled")
      el-button.gradientVertical(size="large" round @click="submitHandle") 确认发布
</template>
<script>
import { mapGetters } from 'vuex'
import { getDirList, dirSubmit, docSubmit } from '../api'

export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      default: 'add',
      type: String
    },
    id: {
      type: [String, Number],
      default: ''
    },
    detail: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      loading: false,
      dirList: [],
      info: {}
    }
  },
  computed: {
    ...mapGetters(['curArea']),
    visibleDialog: {
      get () {
        return this.visible
      },
      set (val) {
        this.$emit('update:visible', val)
      }
    },
    disabled () {
      return this.type === 'view'
    },
    basicFormFields () {
      const { info, dirList } = this
      console.log(info)
      return [
        {
          key: 'title',
          label: '直播名称',
          rules: [{ required: true, message: '请输入' }],
          initialValue: info.title
        },
        {
          key: 'file',
          label: '直播名称',
          rules: [{ required: true, message: '请输入' }],
          type: 'upload',
          fieldProps: {
            imgFlag: true,
            limit: 1,
            accept: 'image/*'
            // 'show-file-list': false
          },
          initialValue: info.resURL ? [{ url: info.resURL, name: info.resURL }] : []
        },
        {
          key: 'liveTime',
          label: '直播时间',
          type: 'datetime',
          initialValue: info.liveTime
        },
        {
          key: 'loginTime',
          label: '允许登录时间',
          type: 'datetime',
          initialValue: info.loginTime
        },
        {
          key: 'url',
          label: '直播地址',
          initialValue: info.url
        },
        {
          key: 'description',
          label: '简介',
          rules: [{ required: true, message: '请输入' }],
          type: 'textarea',
          initialValue: info.description
        }
      ]
    }
  },
  watch: {
    detail (val) {
      console.log(val)
      this.info = val
    }
  },
  created () {
    this.getDirList()
  },
  methods: {
    // 获取目录
    getDirList () {
      getDirList({ type: 2, pageNum: 0, pageSize: 999 }).then(res => {
        this.dirList = res.content || []
      })
    },
    addDirHandle () {
      this.$prompt('输入分类名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        if (value) {
          dirSubmit({ type: 2, title: value }).then(res => {
            this.$message.success('添加目录成功')
            this.getDirList()
          })
        }
      })
    },
    // 提交
    submitHandle () {
      const that = this
      const { id } = this
      this.$confirm('您确认要发布本课程吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const values = await that.$refs.basicForm.submitForm()
        const { file, ...resParams } = values
        console.log(values)
        docSubmit({ ...resParams, id, resURL: file[0].url, resType: file[0].type, audited: 0 }).then(() => {
          that.$message.success('提交成功')
          that.$emit('getList')
          that.$_handleCancel()
        })
      })
    },
    $_handleCancel () {
      this.visibleDialog = false
    }
  }
}
</script>
<style lang="scss" scoped>
.content{
  padding: 0 24px;
  height: calc(100vh - 76px);
  overflow-y: auto;
}
</style>
